package com.yy.controller.BackgroundController;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.yy.config.R;
import com.yy.domain.Admin;
import com.yy.domain.SystemInfo;
import com.yy.service.BackgroundService.impl.BAdminServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Aspect
@RequestMapping(value = "/Badmin")
@Slf4j
public class BAdminController {

    @Autowired
    private BAdminServiceImpl Service;
    @GetMapping("/getSystemInfo")
    public R getSystemInfo() {
        SystemInfo systemInfo = new SystemInfo();
        systemInfo.setSystemVersion("CentOS Linux 7");
        systemInfo.setCpu("16");
        systemInfo.setIpAddress("127.0.0.1");
        systemInfo.setPort("8080");
        systemInfo.setMemory("17179869184");
        return R.ok("系统信息获取成功",systemInfo);
    }

    //AOP查询管理员是否存在不存在则自动插入一条
    @Pointcut("execution(* com.yy.aop.MyCommandLineRunner.run(..))")
    public void startupPointcut() {}
    @Before("startupPointcut()")
    public void AopAdmin(){
        Admin admin = new Admin();
        String salt = RandomUtil.randomString(10);
        //加盐加密
        String password = SecureUtil.md5(salt + "123456");
        admin.setUsername("admin");
        admin.setPassword(password);
        admin.setLv("1");
        admin.setPhone("12345678901");
        admin.setTname("管理员");
        admin.setSalt(salt);
        Boolean b = Service.checkAdmin(admin);
        if (b){
            log.info("账号已存在{}",admin.getUsername());
        }else {
           Service.InsertAdminByAdmin(admin);
           log.info("账号已创建账号为{},密码为{}", admin.getUsername(),admin.getPassword());
        }
    }


    /**
     * 查询管理员信息列表
     * @return
     */
    @RequestMapping(value = "/selectAdminList",method = RequestMethod.POST)
    public List<Admin> selectAdminList(@RequestBody Admin admin){
        return Service.selectAdminList(admin);
    }

    /**
     * 修改管理员信息
     * @param admin
     * @return
     */
    @RequestMapping(value = "/updateAdminById",method = RequestMethod.POST)
    public int updateAdminById(@RequestBody Admin admin){
        return Service.updateAdminById(admin);
    }

    /**
     * 删除管理员信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteAdminById",method = RequestMethod.GET)
    public int deleteAdminById(int id){
        return Service.deleteAdminById(id);
    }

    /**
     * 管理员登录
     * @return
     */
    @RequestMapping(value = "/selectAdminByUser",method = RequestMethod.POST)
    public Admin selectAdminByUser(@RequestBody Admin admin){
        return Service.selectAdminByUser(admin);
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @RequestMapping(value = "/InsertAdminByAdmin",method = RequestMethod.POST)
    public int InsertAdminByAdmin(@RequestBody Admin admin){
        return Service.InsertAdminByAdmin(admin);
    }
}
